package com.societyGames.flashForms
{
	import com.societyGames.system.EventObject;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	
	public class ExpanderContainer extends SingleChildContainer implements IChangeSize
	{
		private var _expanderHeaderContainer:ExpanderHeaderContainer;
		private var _sizeChangedEventObject:EventObject;
		private var _desiredWidth:Number;
		private var _desiredHeight:Number;
		
		public function ExpanderContainer(collapsed:DisplayObject, expanded:DisplayObject, collapsedOver:DisplayObject = null, expandedOver:DisplayObject = null)
		{
			this._sizeChangedEventObject = new EventObject(this, "SIZE_CHANGED");
			this._expanderHeaderContainer = new ExpanderHeaderContainer(collapsed, expanded, collapsedOver, expandedOver);
			this._expanderHeaderContainer.sizeChangedEventObject.addEventListener(expanderHeaderContainerSizeChangedHandler);
			this._expanderHeaderContainer.isExpandedChangedEventObject.addEventListener(expanderHeaderContainerIsExpandedHandler);
			this.addChild(this._expanderHeaderContainer);
		}
		
		private function expanderHeaderContainerSizeChangedHandler(event:Event):void
		{
			if (this._child != null)
			{
				this._child.y = this._expanderHeaderContainer.height;
			}
			this._sizeChangedEventObject.dispatchEvent();
		}
		
		private function expanderHeaderContainerIsExpandedHandler(event:Event):void
		{
			calculateIsExpanded();
		}
		
		private function calculateIsExpanded():void
		{
			this._child.visible = this._expanderHeaderContainer.isExpanded;
			this._sizeChangedEventObject.dispatchEvent();
		}
		
		override public function set child(value:DisplayObject):void
		{
			super.child = value;
			this._child.y = this._expanderHeaderContainer.height;
		}
		
		override protected function childSizeChangedHandler(event:Event):void
		{
			calculateIsExpanded();
		}
		
		public function get headerChild():DisplayObject
		{
			return this._expanderHeaderContainer.child;
		}
		
		public function set headerChild(value:DisplayObject):void
		{
			this._expanderHeaderContainer.child = value;
		}
		
		public function get isExpanded():Boolean
		{
			return this._expanderHeaderContainer.isExpanded;
		}
		
		public function set isExpanded(value:Boolean):void
		{
			this._expanderHeaderContainer.isExpanded = value;
		}
		
		public function get sizeChangedEventObject():EventObject
		{
			return this._sizeChangedEventObject;	
		}
		
		public override function set width(value:Number):void
		{
			if (value != this._desiredWidth)
			{
				this._desiredWidth = value;
				this._expanderHeaderContainer.width = value;
				if (this._child != null)
				{
					this._child.width = this._desiredWidth;
				}
			}
		}
		
		public override function set height(value:Number):void
		{
			if (value != this._desiredHeight)
			{
				this._desiredHeight = value;
				calculateHeight();
			}
		}
		
		private function calculateHeight():void
		{
			//Set scale to default.
			if (this._desiredHeight > this._expanderHeaderContainer.desiredHeight)
			{
				this._expanderHeaderContainer.height = this._expanderHeaderContainer.desiredHeight;
				if (this._child != null)
				{
					this._child.height = this._desiredHeight - this._expanderHeaderContainer.height;
				}
			}
			else //We don't have enough room to display child.
			{
				this._expanderHeaderContainer.height = this._desiredHeight; //Shrink expander.
				this._child.height = 0; //Don't give any room to child.
			}
		}
	}
}